package vn.egame.elockscreen.viewpager;

import vn.egame.elockscreen.R;
import vn.egame.elockscreen.utils.Fonts;
import vn.egame.elockscreen.view.TimeView;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.BounceInterpolator;
import android.view.animation.CycleInterpolator;
import android.view.animation.LinearInterpolator;
import android.view.animation.OvershootInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.lib.shimmer.Shimmer;
import com.lib.shimmer.ShimmerTextView;
import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.Animator.AnimatorListener;
import com.nineoldandroids.animation.AnimatorSet;
import com.nineoldandroids.animation.ObjectAnimator;

public class FragmentUnlock extends BaseFragment {

	private Activity activity;
	private RelativeLayout mRTimeView;
	private TimeView mTimeView;

	private ShimmerTextView shimerUnlock;
	private Shimmer shimmer;
	private IntentFilter screenFilter;
	private LinearLayout llBgUnlock;
	private ImageView imgIconUnlock;

	public FragmentUnlock() {
	}

	@Override
	public void setArguments(Bundle args) {
		super.setArguments(args);

	}

	@Override
	public void onDetach() {
		super.onDetach();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	@Override
	public void onDestroyView() {

		activity.unregisterReceiver(mScreenReceiver);
		super.onDestroyView();
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		activity = getActivity();

		screenFilter = new IntentFilter();
		screenFilter.addAction(Intent.ACTION_SCREEN_OFF);
		screenFilter.addAction(Intent.ACTION_SCREEN_ON);

		activity.registerReceiver(mScreenReceiver, screenFilter);

	}

	AnimatorSet mAnimationSet;
	TranslateAnimation tAnim;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.layout_unlock_fragment, null);

		llBgUnlock = (LinearLayout) view.findViewById(R.id.llBgUnlock);
		setAlpha(0);

		mRTimeView = (RelativeLayout) view.findViewById(R.id.r_timeview);
		shimerUnlock = (ShimmerTextView) view.findViewById(R.id.shimmerUnlock);

//		shimerUnlock.setTypeface(Fonts.getInstance().fontLight);
		shimerUnlock.setText(getResources().getString(R.string.text_unlock));
		shimmer = new Shimmer();
		shimmer.setDuration(3000);
		shimmer.start(shimerUnlock);

		imgIconUnlock = (ImageView) view.findViewById(R.id.imgIconUnlock);
		// YoYo.with(Techniques.FadeOut).duration(1).playOn(imgIconUnlock);

		ObjectAnimator fadeOut = ObjectAnimator.ofFloat(imgIconUnlock, "alpha",
				1f, .3f);
		fadeOut.setDuration(2000);
		ObjectAnimator fadeIn = ObjectAnimator.ofFloat(imgIconUnlock, "alpha",
				.3f, 1f);
		fadeIn.setDuration(2000);

		mAnimationSet = new AnimatorSet();

		mAnimationSet.play(fadeIn).after(fadeOut);

		mAnimationSet.addListener(new AnimatorListener() {

			@Override
			public void onAnimationStart(Animator arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationRepeat(Animator arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationEnd(Animator arg0) {
				// TODO Auto-generated method stub
				mAnimationSet.start();
			}

			@Override
			public void onAnimationCancel(Animator arg0) {
				// TODO Auto-generated method stub

			}
		});
		mAnimationSet.start();

		tAnim = new TranslateAnimation(-50, 0f, 0f, 0f);
		tAnim.setDuration(2000);
		tAnim.setRepeatCount(-1);
		tAnim.setInterpolator(new OvershootInterpolator());
		// tAnim.setAnimationListener(new AnimationListener() {
		// @Override
		// public void onAnimationStart(Animation animation) {
		// imgIconUnlock.startAnimation(tAnim);
		// }
		//
		// @Override
		// public void onAnimationRepeat(Animation animation) {
		// }
		//
		// @Override
		// public void onAnimationEnd(Animation animation) {
		// }
		// });
		this.imgIconUnlock.startAnimation(tAnim);

		mTimeView = new TimeView(activity);
		RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT,
				RelativeLayout.LayoutParams.WRAP_CONTENT);
		mRTimeView.addView(mTimeView, params);
		return view;
	}

	private BroadcastReceiver mScreenReceiver = new BroadcastReceiver() {
		public void onReceive(Context paramAnonymousContext,
				Intent paramAnonymousIntent) {
			String str = paramAnonymousIntent.getAction();
			if (str.equals("android.intent.action.SCREEN_ON")) {

				Log.d("Kai", "FragmentUnlock SCREEN_ON");

				shimmer.start(shimerUnlock);
				// shimerUnlock.setVisibility(View.VISIBLE);
				tAnim.start();
				mAnimationSet.start();
			}
			if (str.equals("android.intent.action.SCREEN_OFF")) {
				Log.d("Kai", "FragmentUnlock SCREEN_OFF");
				shimmer.cancel();
				tAnim.cancel();
				mAnimationSet.cancel();
				// shimerUnlock.setVisibility(View.INVISIBLE);

			}
		}
	};

	public void setAlpha(float alpha) {
		llBgUnlock.setAlpha(alpha);
	}

	// @Override
	// public void setUserVisibleHint(boolean isVisibleToUser) {
	// super.setUserVisibleHint(isVisibleToUser);
	//
	// // Make sure that we are currently visible
	// if (this.isVisible()) {
	// // If we are becoming invisible, then...
	// setAlpha(0);
	// }
	// }

}
